package com.sanji.sjmallmodule.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigAPI;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.http.NoHttpCallBack;
import com.sanji.sjmallmanager.http.NoHttpListener;
import com.sanji.sjmallutils.Tools;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 * 修改密码
 */
public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener, NoHttpListener {
    @BindView(R.id.change_password_btn)
    Button changePasswordOkBtn;
    @BindView(R.id.password_edit)
    EditText passwordEdit;
    @BindView(R.id.repeat_password_edit)
    EditText repeatPasswordEdit;
    private String passwordNumber;
    private String repeatPasswordNumber;
    private TextView commonTitle;
    private ImageView backImage;
    private Request<JSONObject> request;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        commonTitle = (TextView) findViewById(R.id.common_title_tv);
        commonTitle.setVisibility(View.VISIBLE);
        backImage = (ImageView) findViewById(R.id.common_back_img);
        commonTitle.setText(getString(R.string.password_title));
        backImage.setOnClickListener(this);
        changePasswordOkBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back_img:
                finish();
                break;
            case R.id.change_password_btn:
                changPasswordRequest();
                break;
        }

    }

    /**
     * 确实修改密码
     */
    public void changPasswordRequest() {
        passwordNumber = passwordEdit.getText().toString().trim();
        repeatPasswordNumber = repeatPasswordEdit.getText().toString().trim();
        if (TextUtils.isEmpty(passwordNumber)) {
            Tools.toastTools(mCtx, getString(R.string.password_new_not_null));
            return;
        }
        if (!(passwordNumber.equals(repeatPasswordNumber))) {
            Tools.toastTools(mCtx, getString(R.string.password_second_error));
            return;
        }
        if (!TextUtils.isEmpty(passwordNumber) && (passwordNumber.equals(repeatPasswordNumber))) {
            //发送网络请求
            String account = myShare.getString(ConfigConts.ACCOUNT, null);
            String url = ConfigAPI.CHANGE_PASSWORD_URL + account + "?";
            request = NoHttp.createJsonObjectRequest(url, RequestMethod.POST);
            JSONObject json = new JSONObject();
            try {
                json.put("password", passwordNumber);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            request.setDefineRequestBodyForJson(json);
            request.setCancelSign(ConfigConts.ACCOUNT.hashCode());
            NoHttpCallBack.getInstance().add(mCtx, ConfigAPI.CHANGE_PASSWORD_URL.hashCode(), request, this, true, true);
        }
    }

    @Override
    public void onSuccessed(int what, Response response) {
        //修改成功跳转到登录页
        int requestCode = response.getHeaders().getResponseCode();
        JSONObject jsonObject = (JSONObject) response.get();
        if (requestCode == 200) {
            String status = jsonObject.optString(ConfigConts.STATUS_STRING);
            if (status.equals(ConfigConts.STATUS_SUCCESS)) {
                String successMsg = jsonObject.optString(ConfigConts.SUCCESSMSG);
                Tools.toastTools(mCtx, successMsg);
                intent = new Intent(ChangePasswordActivity.this, LoginActivity.class);
                startActivity(intent);
            } else {
                String errorMsg = jsonObject.optString(ConfigConts.ERRORMSG);
                Tools.toastTools(mCtx, errorMsg);
            }
        } else {
            Tools.toastTools(mCtx, getString(R.string.password_modify_false));
        }

    }

    @Override
    public void onFailed(int what, Response response) {
        Tools.toastTools(mCtx, getString(R.string.request_false));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        request.cancel();
    }
}
